package uk.co.richardwkirk.parkrun.android.storage;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class AthleteDataSource {

	// Database fields
	private AthleteOpenHelper dbHelper = null;
	private String[] listDetails = { AthleteOpenHelper.ATHLETE_ID, AthleteOpenHelper.ATHLETE_NAME, AthleteOpenHelper.ATHLETE_CLUB };
	private String[] allColumns = { AthleteOpenHelper.ATHLETE_ID, AthleteOpenHelper.ATHLETE_NAME, AthleteOpenHelper.ATHLETE_CLUB, AthleteOpenHelper.ATHLETE_DATA };

	public AthleteDataSource(Context context) {
		dbHelper = new AthleteOpenHelper(context);
	}

	public void open() {
		// Nothing required.
	}
	
	public void close() {
		dbHelper.close();
	}

	public AthleteData createAthlete(long number) {
		// Insert a new athlete entry into the DB
		ContentValues values = new ContentValues();
		values.put(AthleteOpenHelper.ATHLETE_ID, number);
		values.put(AthleteOpenHelper.ATHLETE_NAME, (String) null);
		values.put(AthleteOpenHelper.ATHLETE_CLUB, (String) null);
		values.put(AthleteOpenHelper.ATHLETE_DATA, (String) null);
		dbHelper.getWritableDatabase().insert(AthleteOpenHelper.ATHLETE_LIST_TABLE_NAME, null, values);
		return getAthlete(number);
	}

	public AthleteData getAthlete(long id) {
		// Query for an insert item by id
		Cursor cursor = dbHelper.getReadableDatabase().query(AthleteOpenHelper.ATHLETE_LIST_TABLE_NAME, allColumns, AthleteOpenHelper.ATHLETE_ID + " = " + String.valueOf(id), null, null, null, null);
		AthleteData newEntry = null;
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			newEntry = cursorToEntry(cursor);
			cursor.close();
		}
		return newEntry;
	}

	public void deleteAthlete(AthleteData entry) {
		long id = entry.getId();
		System.out.println("Athlete deleted with id: " + id);
		dbHelper.getWritableDatabase().delete(AthleteOpenHelper.ATHLETE_LIST_TABLE_NAME, AthleteOpenHelper.ATHLETE_ID + " = " + id, null);
	}

	public List<AthleteData> getAthleteList() {
		List<AthleteData> allAthletes = new ArrayList<AthleteData>();

		// Query for all athletes
		Cursor cursor = dbHelper.getReadableDatabase().query(AthleteOpenHelper.ATHLETE_LIST_TABLE_NAME, listDetails, null, null, null, null, null);

		// Fill the list of athletes
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			AthleteData entry = cursorToEntry(cursor);
			allAthletes.add(entry);
			cursor.moveToNext();
			// Log.i("PARKRUN", "Found entry " + entry.getId() + " : " + entry.getName());
		}

		// Make sure to close the cursor
		cursor.close();
		return allAthletes;
	}

	private AthleteData cursorToEntry(Cursor cursor) {
		AthleteData entry = new AthleteData();
		entry.setId(cursor.getLong(0));
		entry.setName(cursor.getString(1));
		entry.setClub(cursor.getString(2));
		if (cursor.getColumnCount() > 3) {
			entry.setData(cursor.getString(3));
		}
		return entry;
	}

	public AthleteData updateAthlete(AthleteData athlete) {
		// Insert a new athlete entry into the DB
		ContentValues values = new ContentValues();
		values.put(AthleteOpenHelper.ATHLETE_ID, athlete.id);
		values.put(AthleteOpenHelper.ATHLETE_NAME, athlete.name);
		values.put(AthleteOpenHelper.ATHLETE_CLUB, athlete.club);
		values.put(AthleteOpenHelper.ATHLETE_DATA, athlete.data);
		dbHelper.getWritableDatabase().update(AthleteOpenHelper.ATHLETE_LIST_TABLE_NAME, values, AthleteOpenHelper.ATHLETE_ID + " = " + String.valueOf(athlete.id), null);
		return getAthlete(athlete.id);
	}



}